<?php

class DashboardController extends Zend_Controller_Action {

	public function indexAction() {
		
	}
	
	public function settingAction() {
		$form = new Default_Form_Setting();
		$form->setAction($this->view->url(array(),'default_dashboard_setting'));
		
		$this->view->form = $form;
		
		$setting = Doctrine_Core::getTable('Default_Model_Setting')->find(1);
		
		if($setting) {
			$populate = array(
				'own_username' => $setting->own_username,
				'own_email' => $setting->own_email,
				'is_smtp' => $setting->is_smtp,
				'own_smtp_login' => $setting->own_smtp_login,
				'own_smtp_password' => $setting->own_smtp_password,
				'email_template_password' => $setting->Translation['ru']->email_template_password,
				'email_template_register' => $setting->Translation['ru']->email_template_register,
				'own_smtp_host' => $setting->own_smtp_host,
			);
		}
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			if(!$setting) {
				$setting = new Default_Model_Setting();
			}
			$setting->own_username = $values['own_username'];
			$setting->own_email = $values['own_email'];
			$setting->is_smtp = $values['is_smtp'];
			$setting->own_smtp_login = $values['own_smtp_login'];
			$setting->own_smtp_password = $values['own_smtp_password'];
			$setting->Translation['ru']->email_template_password = $values['email_template_password'];
			$setting->Translation['ru']->email_template_register = $values['email_template_register'];
			$setting->own_smtp_host = $values['own_smtp_host'];
			$setting->save();
			$this->_helper->getHelper('FlashMessenger')
						->addMessage($this->view->translate('Настройки успешно сохранены!'));
			return $this->_redirect($this->view->url(array(), 'default_dashboard_setting'));
			
		} else {
			if($setting) {
				$form->populate($populate);
			}
		}
	}
}
